<template>
  <div class="login">
    <div style="z-index: 99; position: absolute; top: 15%;font-size: 30px; font-weight: bold;">
      <h1 style="color: white;">立木生物量数据采集系统</h1>
      <br>
    </div>
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h2 class="title">欢迎登录</h2>

      <el-form-item prop="username">

        <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
          <template #prefix><svg-icon icon="user" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码">
          <template #prefix><svg-icon icon="password" /></template>
        </el-input>
      </el-form-item>

      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
          <span>登 录</span>
        </el-button>

      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <Footer />
    </div>
  </div>
</template>

<script setup>
import Footer from '@/layout/footer/index.vue'
import { ref } from 'vue'
import api from '@/util/request'
import qs from 'qs'
import { ElMessage } from 'element-plus'
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/util/jsencrypt";
import router from '@/router'
// import store from '@/store'

// 获取全局状态管理信息
import { useUserStore } from '@/stores/user';
const { token, currentUser } = useUserStore();
const UserStore = useUserStore()

if (token) {
  ElMessage.success('您已处于登录状态！')
  if (window.history.state.back) {
    router.back()
  } else {
    router.push('/') // 返回首页
  }
}

const loginForm = ref({
  username: '',
  password: '',
  rememberMe: false
})

const loginRef = ref(null)

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号！" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码！" }]
};

const handleLogin = () => {
  loginRef.value.validate(async (valid) => {
    if (valid) {
      let result = await api.post("user/login?" + qs.stringify(loginForm.value))
      let data = result.data
      if (data.code == 200) {
        ElMessage.success(data.info)
        window.sessionStorage.setItem("token", data.token)
        const loginUser = data.user
        loginUser.roles = data.roles
        UserStore.currentUser = loginUser
        window.sessionStorage.setItem("currentUser", JSON.stringify(loginUser))
        window.sessionStorage.setItem("menuList", JSON.stringify(data.menuList))
        // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
        if (loginForm.value.rememberMe) {
          Cookies.set("username", loginForm.value.username, { expires: 30 });
          Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
          Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
        } else {
          // 否则移除
          Cookies.remove("username");
          Cookies.remove("password");
          Cookies.remove("rememberMe");
        }
        // router.replace("/admin")
        if (window.history.state.back) {
          router.back()
        } else {
          router.replace('/') // 返回首页
        }
      } else {
        ElMessage.error(data.info)
      }
    } else {
      console.log("验证失败!")
    }
  })
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
}

getCookie();

</script>

<style lang="scss" scoped>
a {
  color: white
}

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  // background-image: url("../assets/images/login-background.jpg");
  background-image: linear-gradient(to bottom, rgba(0, 49, 29, 0.3), rgba(0, 0, 0, 0.8)), url("@/assets/images/login-background.jpg");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  font-weight: bold;
  font-size: 18px;
  color: #353535;
}

.login-form {
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.6);
  width: 400px;
  padding: 40px 80px;

  .el-input {
    height: 40px;
    --el-input-bg-color: rgba(255, 255, 255, 0.3);

    input {
      display: inline-block;
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}

.svg-icon {
  fill: #464646;
}

.login-form:hover {
  box-shadow: 0px 0px 10px rgba(255, 255, 255, 0.5);
}

.el-checkbox {
  --el-checkbox-bg-color: rgba(255, 255, 255, 0.3) !important;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  position: fixed;
  bottom: 0;
  margin-bottom: 20px;
  width: 100%;
  text-align: center;
  font-family: Arial;
  letter-spacing: 1px;
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
